//
//  AffairsPublicViewController.m
//  ASHT_IOS
//
//  Created by 宋安安 on 14-4-8.
//  Copyright (c) 2014年 宋安安. All rights reserved.
//

#import "AffairsPublicViewController.h"
#import "ZWGKThirdViewController.h"
#import "WorkServicesCell.h"
#import "ZWGKEntity.h"
#import "AFAppDotNetAPIClient.h"
#import "NewsInfoEntity.h"
#import "MBProgressHUD.h"
@interface AffairsPublicViewController ()

@end
@implementation AffairsPublicViewController

@synthesize dataArray;
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
   
        _Adaptation = YES;
    }
    return self;
    
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    dataArray = [[NSMutableArray alloc] init];
    zwgkTableView.delegate = self;
    zwgkTableView.dataSource = self;
    [self getZWGKData];

}

-(void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    if ([[[UIDevice currentDevice] systemVersion] floatValue] >= 7)
    {
        if (_Adaptation) {
            self.view_Content.frame=CGRectMake(self.view_Content.frame.origin.x,self.view_Content.frame.origin.y,self.view_Content.frame.size.width,self.view_Content.frame.size.height-20);
            _Adaptation=NO;
        }
        
    }
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

-(NSInteger) tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    tableView.separatorStyle = UITableViewCellAccessoryNone;
    return dataArray.count;
}

-(UITableViewCell *) tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    NSUInteger row = [indexPath row];
	static NSString *CellIdentifier = @"Cell";
	
	WorkServicesSubCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if(cell == nil)
	{
        NSArray *nibs=[[NSBundle mainBundle] loadNibNamed:@"WorkServicesCell" owner:self options:nil];
        if (nibs.count>0) {
            cell =[nibs objectAtIndex:1];
            nibs= nil;
        }
        
    }
    
	//行是否允许点击事件
	tableView.allowsSelection=YES;
    
	ZWGKEntity *entity = [self.dataArray objectAtIndex:row];
	cell.lab_cellname.text = entity.categoryName;
    
    return cell;
}

-(void) tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    int newIndex = [indexPath row];
    int lastIndex = (lastIndexPath != nil) ? [lastIndexPath row] : -1;
    if (newIndex != lastIndex) {
        WorkServicesSubCell *cell= (WorkServicesSubCell*)[tableView cellForRowAtIndexPath:indexPath];
        CGRect rect = cell.lab_cellname.frame;
        rect.origin.x = cell.lab_cellname.frame.origin.x + 10;
        cell.lab_cellname.frame  = rect;
        
        cell= (WorkServicesSubCell*)[tableView cellForRowAtIndexPath:lastIndexPath];
        CGRect rect1 = cell.lab_cellname.frame;
        rect1.origin.x = cell.lab_cellname.frame.origin.x - 10;
        cell.lab_cellname.frame  = rect1;
        lastIndexPath = indexPath;
    }
    int row = [indexPath row];
    ZWGKEntity *serviceEntity = [self.dataArray objectAtIndex:row];
    NSString *categoryId = serviceEntity.categoryId;
    zwgkThirdVC = [[ZWGKThirdViewController alloc] initWithNibName:XIB_Version(@"ZWGKThirdViewController") bundle:nil];
    zwgkThirdVC.categoryId = categoryId;
    [self.app.viewDeckController.navigationController pushViewController:zwgkThirdVC animated:YES];
    
}
-(void)viewWillDisappear:(BOOL)animated
{
    [super viewWillDisappear:animated];
 
    isarler=YES;
    
}
-(void) getZWGKData
{
    NSString *url=@"phone!getZwgkCategoryList.action";
    [MBProgressHUD showHUDAddedTo:self.view animated:YES];
    [[AFAppDotNetAPIClient sharedClient] postPath:url parameters:nil success:^(AFHTTPRequestOperation *operation, id JSON) {
        NSDictionary *dic_result=[NSDictionary dictionaryWithDictionary:JSON];
        NSArray *arr_success=[dic_result objectForKey:@"context"];
        
        
        for (NSDictionary * item in arr_success) {
            ZWGKEntity *entity = [[ZWGKEntity alloc] init];
            entity.categoryFlag = [item objectForKey:@"categoryFlag"];
            entity.categoryId = [item objectForKey:@"categoryId"];
            entity.categoryName = [item objectForKey:@"categoryName"];
            entity.imageUrl = [item objectForKey:@"imageUrl"];
            [self.dataArray addObject:entity];
            entity = nil;
            
        }
        [zwgkTableView reloadData];
        [MBProgressHUD hideAllHUDsForView:self.view animated:YES];
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        [MBProgressHUD hideAllHUDsForView:self.view animated:YES];
        NSLog(@"%@",error);
        NSString *message=[self ErrorMessageHandling:error];
        UIAlertView *alert=[[UIAlertView alloc] initWithTitle:@"提示"
                                                      message:message
                                                     delegate:nil
                                            cancelButtonTitle:@"确定"
                                            otherButtonTitles:nil];
        [alert show];
        alert=nil;

    }];
}



@end
